package com.eonmind.user.mapper;


import com.eonmind.common.contract.data.base.BaseMongoMapper;
import com.eonmind.common.contract.enums.DelEnum;
import com.eonmind.common.contract.enums.user.UserAccountStatus;
import com.eonmind.common.provider.entity.user.User;
import com.mongoplus.conditions.query.QueryWrapper;
import com.mongoplus.conditions.update.UpdateWrapper;
import org.springframework.stereotype.Repository;

@Repository
public class UserMapper extends BaseMongoMapper<User> {
    public User selectUserByEmail(String mail) {
        QueryWrapper<User> eq = createMgoQuery()
                .eq(User::getEmail, mail)
                .eq(User::getStatus, UserAccountStatus.NORMAL.getCode())
                .eq(User::getDel, DelEnum.UN_DEL.getCode());
        return one(eq);
    }

    public User selectUserByUserName(String userName) {
        QueryWrapper<User> eq = createMgoQuery()
                .eq(User::getUserName, userName)
                .eq(User::getStatus, UserAccountStatus.NORMAL.getCode())
                .eq(User::getDel, DelEnum.UN_DEL.getCode());
        return one(eq);
    }

    public User selectUserByPhone(String phone) {
        QueryWrapper<User> eq = createMgoQuery()
                .eq(User::getPhoneNumber, phone)
                .eq(User::getStatus, UserAccountStatus.NORMAL.getCode())
                .eq(User::getDel, DelEnum.UN_DEL.getCode());
        return one(eq);
    }

    public User selectUserById(Long id) {
        QueryWrapper<User> eq = createMgoQuery()
                .eq(User::getId, id)
                .eq(User::getStatus, UserAccountStatus.NORMAL.getCode())
                .eq(User::getDel, DelEnum.UN_DEL.getCode());
        return one(eq);
    }


    public boolean existsByUsername(String username) {
        QueryWrapper<User> eq = createMgoQuery()
                .eq(User::getUserName, username)
                .eq(User::getDel, DelEnum.UN_DEL.getCode());
        return one(eq) != null;
    }

    public boolean existsByPhoneNumber(String phoneNumber) {
        QueryWrapper<User> eq = createMgoQuery()
                .eq(User::getPhoneNumber, phoneNumber)
                .eq(User::getDel, DelEnum.UN_DEL.getCode());
        return one(eq) != null;
    }

    public boolean existsByEmail(String email) {
        QueryWrapper<User> eq = createMgoQuery()
                .eq(User::getEmail, email)
                .eq(User::getDel, DelEnum.UN_DEL.getCode());
        return one(eq) != null;
    }

    public boolean updatePassword(String email, String password, Integer passwordComplexity) {
        UpdateWrapper<User> update = createMgoUpdate()
                .set(User::getPassword, password)
                .set(User::getUpdateTime, System.currentTimeMillis())
                .set(User::getPasswordComplexity, passwordComplexity)
                .eq(User::getEmail, email);
        update(update);
        return true;
    }
}